<?php
//公共函数库
/*function toDate($time, $format = 'Y-m-d H:i:s') {
	if (empty ( $time )) {
		return '';
	}
	$format = str_replace ( '#', ':', $format );
	return date ($format, $time );
}

function get_client_ip() {
	if (getenv ( "HTTP_CLIENT_IP" ) && strcasecmp ( getenv ( "HTTP_CLIENT_IP" ), "unknown" ))
		$ip = getenv ( "HTTP_CLIENT_IP" );
	else if (getenv ( "HTTP_X_FORWARDED_FOR" ) && strcasecmp ( getenv ( "HTTP_X_FORWARDED_FOR" ), "unknown" ))
		$ip = getenv ( "HTTP_X_FORWARDED_FOR" );
	else if (getenv ( "REMOTE_ADDR" ) && strcasecmp ( getenv ( "REMOTE_ADDR" ), "unknown" ))
		$ip = getenv ( "REMOTE_ADDR" );
	else if (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], "unknown" ))
		$ip = $_SERVER ['REMOTE_ADDR'];
	else
		$ip = "unknown";
	return ($ip);
}
function IP($ip = '', $file = 'UTFWry.dat') {
	$_ip = array ();
	if (isset ( $_ip [$ip] )) {
		return $_ip [$ip];
	} else {
		import ( "ORG.Net.IpLocation" );
		$iplocation = new IpLocation( $file );
		$location = $iplocation->getlocation ( $ip );
		$_ip [$ip] = $location ['country'] . $location ['area'];
	}
	return $_ip [$ip];
}

function getCardStatus($status) {
	switch ($status) {
		case 0 :
			$show = '未启用';
			break;
		case 1 :
			$show = '已启用';
			break;
		case 2 :
			$show = '使用中';
			break;
		case 3 :
			$show = '已禁用';
			break;
		case 4 :
			$show = '已作废';
			break;
	}
	return $show;

}*/
//截取字符串的长度utf-8
function utf_substr($str, $start, $len) {
	if (strlen ( $str ) < $start) {
		return "";
	}
	$start = (ceil ( $start / 3 )) * 3;
	$str = substr($str,$start);
	
	for($i = 0; $i < $len; $i ++) {
		$temp_str = substr ( $str, 0, 1 );
		if (ord ( $temp_str ) > 127) {
			$i ++;
			if ($i < $len) {
				$new_str [] = substr ( $str, 0, 3 );
				$str = substr ( $str, 3 );
			}
		} else {
			$new_str [] = substr ( $str, 0, 1 );
			$str = substr ( $str, 1 );
		}
	}
	if (count ( $new_str ) > 1) {
		return join ( $new_str );
	} else {
		return $new_str [0];
	}
}


//获取格式化后他主页图标
function formatIconUrl($url=NULL){
	if(empty($url)){
		return 'http://6.su.bdimg.com/icon/6000.png';
	}
    if (strpos($url, 'http://') !== 0) {
		return 'http://www.3y3.com/' . ltrim($url, '/');
	}
	$parse_url = parse_url($url);
	$url = 'http://api.tao123.com/collectsite/ico.php?domain_url='.$parse_url['scheme'].'://'.$parse_url['host'];
	return $url;
}
?>